package net.tngou.iask.action.api;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang3.math.NumberUtils;

import net.tngou.iask.action.BaseAction;
import net.tngou.iask.enums.LevelEnum;
import net.tngou.iask.pojo.Agerange;
import net.tngou.iask.pojo.Department;
import net.tngou.iask.pojo.Disease;
import net.tngou.iask.pojo.POJO;
import net.tngou.iask.pojo.Place;
import net.tngou.iask.pojo.Treatment;
import net.tngou.iask.service.DiseaseService;

/**
 * 疾病操作接口
 * @author tngou@tngou.net
 *
 */
public class DiseaseApi extends BaseAction {
	
	/**
	 * 列表
	 */
	public void list() {
		
	}
	
	/**
	 * 添加疾病
	 */
	public void add() {
		if(user==null){
			run_nologin();return;
		}
		if(user.getRole()!=1&&user.getRole()!=2&&user.getRole()!=3)
		{
			run_false("没有添加权限");return;
		}
		Disease bean = getAsk(Disease.class);
		if(isEmpty(bean.getName()))
		{
			run_false("请求的参数不正确！");return;
		}
		@SuppressWarnings("serial")
		POJO pojo = bean.get(new HashMap<String,Object>(){{put("name", bean.getName());}});
		if(pojo!=null)
		{
			run_false("该疾病的名称已经存在！");return;
		}
		long id = bean.save();
		if(id>0){
			String[] place=request.getParameterValues("places");//取得发病位置
			String[] agerange=request.getParameterValues("ageranges");//取得年龄范围
			String[] treatment=request.getParameterValues("treatments");//取得治疗方式
			String[] department=request.getParameterValues("departments");//取得相关科室
			DiseaseService service = new DiseaseService();
			service.update(id, place, agerange, treatment, department);	
			saveLog(LevelEnum.system, "添加疾病(id:"+id+"-"+bean.getName()+")");
			run_success("添加成功！");
		}
		
		else run_false("添加失败！");
		
	}
	/**
	 * 更新疾病
	 */
	public void update() {
		if(user==null){
			run_nologin();return;
		}
		if(user.getRole()!=1&&user.getRole()!=2&&user.getRole()!=3)
		{
			run_false("没有添加权限");return;
		}
		Disease bean = getAsk(Disease.class);
		if(bean.getId()==0)
		{
			run_false("请求的参数不正确！");return;
		}
		Disease disease = new Disease().get(bean.getId());
		if(disease==null)
		{
			run_false("请求的参数不正确！");return;
		}
		if(!disease.getName().equals(bean.getName()))
		{
			@SuppressWarnings("serial")
			POJO pojo = bean.get(new HashMap<String,Object>(){{put("name", bean.getName());}});
			if(pojo!=null)
			{
				run_false("该疾病的名称已经存在！");return;
			}
		}
		try {
			Map<String, Object> map = PropertyUtils.describe(bean);
			map.remove("id");
			map.remove("class");
			Object[] keys = map.keySet().toArray();	
		    for (Object key : keys) {
		    	if(map.get(key)==null){
			    	map.remove(key);
			    }else{
			    	
			    	if(NumberUtils.isNumber(map.get(key).toString())
			    			&&NumberUtils.createNumber(map.get(key).toString()).intValue()==0)
			    	{
			    		map.remove(key);
			    	}
			    }
			}
		  long id = bean.update(map,disease.getId());
		  if(id>0){
			    String[] place=request.getParameterValues("places");//取得发病位置
				String[] agerange=request.getParameterValues("ageranges");//取得年龄范围
				String[] treatment=request.getParameterValues("treatments");//取得治疗方式
				String[] department=request.getParameterValues("departments");//取得相关科室
				DiseaseService service = new DiseaseService();
				service.update(disease.getId(), place, agerange, treatment, department);	
				saveLog(LevelEnum.system, "修改疾病(id:"+bean.getId()+"-"+bean.getName()+")");
			  run_success("修改成功！");
			  }
			else {run_false("修改失败！");}
			
		} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
			e.printStackTrace();
			run_false("患者更新失败！");return;
		}
	}
	
	/**
	 * 添加身体位置
	 */
	public void placeadd() {
		if(user==null){
			run_nologin();return;
		}
		if(user.getRole()!=1&&user.getRole()!=2)
		{
			run_false("没有修改权限");return;
		}
		Place bean = getAsk(Place.class);
		if(isEmpty(bean.getName()))
		{
			run_false("请求的参数不正确！");return;
		}
		@SuppressWarnings("serial")
		POJO pojo = bean.get(new HashMap<String,Object>(){{put("name", bean.getName());}});
		if(pojo!=null)
		{
			run_false("该身体的名称已经存在！");return;
		}
		long id = bean.save();
		if(id>0){
			saveLog(LevelEnum.system, "添加身体位置(id:"+id+"-"+bean.getName()+")");
			run_success("添加成功！");
		}
		else{ run_false("添加失败！");}
	}
	/**
	 * 更新身体位置
	 */
	public void placeupdate() {
		if(user==null){
			run_nologin();return;
		}
		if(user.getRole()!=1&&user.getRole()!=2)
		{
			run_false("没有修改权限");return;
		}
		Place bean = getAsk(Place.class);
		if(bean.getId()==0)
		{
			run_false("请求的参数不正确！");return;
		}
		Place place = new Place().get(bean.getId());
		if(place==null)
		{
			run_false("请求的参数不正确！");return;
		}
		if(!place.getName().equals(bean.getName()))
		{
			@SuppressWarnings("serial")
			POJO pojo = bean.get(new HashMap<String,Object>(){{put("name", bean.getName());}});
			if(pojo!=null)
			{
				run_false("该身体位置的名称已经存在！");return;
			}
		}
		place.setName(bean.getName());
		long id = place.update();
		 if(id>0){
			saveLog(LevelEnum.system, "修改身体位置(id:"+id+"-"+bean.getName()+")");
			run_success("修改成功！");
		}else {run_false("修改失败！");}
		
	}
	
	/**
	 * 添加就诊科室
	 */
	public void departmentadd() {
		if(user==null){
			run_nologin();return;
		}
		if(user.getRole()!=1&&user.getRole()!=2)
		{
			run_false("没有修改权限");return;
		}
		Department bean = getAsk(Department.class);
		if(isEmpty(bean.getName()))
		{
			run_false("请求的参数不正确！");return;
		}
		@SuppressWarnings("serial")
		POJO pojo = bean.get(new HashMap<String,Object>(){{put("name", bean.getName());}});
		if(pojo!=null)
		{
			run_false("该科室的名称已经存在！");return;
		}
		long id = bean.save();
		if(id>0){
			saveLog(LevelEnum.system, "添加就诊科室(id:"+id+"-"+bean.getName()+")");
			run_success("添加成功！");
		}
		else{ run_false("添加失败！");}
	}
	/**
	 * 更新就诊科室
	 */
	public void departmentupdate() {
		if(user==null){
			run_nologin();return;
		}
		if(user.getRole()!=1&&user.getRole()!=2)
		{
			run_false("没有修改权限");return;
		}
		Department bean = getAsk(Department.class);
		if(bean.getId()==0)
		{
			run_false("请求的参数不正确！");return;
		}
		Department department = new Department().get(bean.getId());
		if(department==null)
		{
			run_false("请求的参数不正确！");return;
		}
		if(!department.getName().equals(bean.getName()))
		{
			@SuppressWarnings("serial")
			POJO pojo = bean.get(new HashMap<String,Object>(){{put("name", bean.getName());}});
			if(pojo!=null)
			{
				run_false("该就诊科室的名称已经存在！");return;
			}
		}
		department.setName(bean.getName());
		long id = department.update();
		 if(id>0){
			saveLog(LevelEnum.system, "修改就诊科室(id:"+bean.getId()+"-"+bean.getName()+")");
			run_success("修改成功！");
		}else {run_false("修改失败！");}
		
	}
	
	/**
	 * 添加年龄分段
	 */
	public void agerangeadd() {
		if(user==null){
			run_nologin();return;
		}
		if(user.getRole()!=1&&user.getRole()!=2)
		{
			run_false("没有修改权限");return;
		}
		Agerange bean = getAsk(Agerange.class);
		if(isEmpty(bean.getName()))
		{
			run_false("请求的参数不正确！");return;
		}
		@SuppressWarnings("serial")
		POJO pojo = bean.get(new HashMap<String,Object>(){{put("name", bean.getName());}});
		if(pojo!=null)
		{
			run_false("该年龄段的名称已经存在！");return;
		}
		long id = bean.save();
		if(id>0){
			saveLog(LevelEnum.system, "添加年龄段(id:"+id+"-"+bean.getName()+")");
			run_success("添加成功！");
		}
		else{ run_false("添加失败！");}
	}
	/**
	 * 更新年龄段
	 */
	public void agerangeupdate() {
		if(user==null){
			run_nologin();return;
		}
		if(user.getRole()!=1&&user.getRole()!=2)
		{
			run_false("没有修改权限");return;
		}
		Agerange bean = getAsk(Agerange.class);
		if(bean.getId()==0)
		{
			run_false("请求的参数不正确！");return;
		}
		Agerange agerange = new Agerange().get(bean.getId());
		if(agerange==null)
		{
			run_false("请求的参数不正确！");return;
		}
		if(!agerange.getName().equals(bean.getName()))
		{
			@SuppressWarnings("serial")
			POJO pojo = bean.get(new HashMap<String,Object>(){{put("name", bean.getName());}});
			if(pojo!=null)
			{
				run_false("该年龄段的名称已经存在！");return;
			}
		}
		agerange.setName(bean.getName());
		agerange.setStartage(bean.getStartage());
		agerange.setEndage(bean.getEndage());
		long id = agerange.update();
		 if(id>0){
			saveLog(LevelEnum.system, "修改年龄段(id:"+bean.getId()+"-"+bean.getName()+")");
			run_success("修改成功！");
		}else {run_false("修改失败！");}
		
	}
	
	/**
	 * 添加治疗方式
	 */
	public void treatmentadd() {
		if(user==null){
			run_nologin();return;
		}
		if(user.getRole()!=1&&user.getRole()!=2)
		{
			run_false("没有修改权限");return;
		}
		Treatment bean = getAsk(Treatment.class);
		if(isEmpty(bean.getName()))
		{
			run_false("请求的参数不正确！");return;
		}
		@SuppressWarnings("serial")
		POJO pojo = bean.get(new HashMap<String,Object>(){{put("name", bean.getName());}});
		if(pojo!=null)
		{
			run_false("该年龄段的名称已经存在！");return;
		}
		long id = bean.save();
		if(id>0){
			saveLog(LevelEnum.system, "添加治疗方式(id:"+id+"-"+bean.getName()+")");
			run_success("添加成功！");
		}
		else{ run_false("添加失败！");}
	}
	/**
	 * 更新治疗方式
	 */
	public void treatmentupdate() {
		if(user==null){
			run_nologin();return;
		}
		if(user.getRole()!=1&&user.getRole()!=2)
		{
			run_false("没有修改权限");return;
		}
		Treatment bean = getAsk(Treatment.class);
		if(bean.getId()==0)
		{
			run_false("请求的参数不正确！");return;
		}
		Treatment treatment = new Treatment().get(bean.getId());
		if(treatment==null)
		{
			run_false("请求的参数不正确！");return;
		}
		if(!treatment.getName().equals(bean.getName()))
		{
			@SuppressWarnings("serial")
			POJO pojo = bean.get(new HashMap<String,Object>(){{put("name", bean.getName());}});
			if(pojo!=null)
			{
				run_false("该治疗方式的名称已经存在！");return;
			}
		}
		treatment.setName(bean.getName());
		long id = treatment.update();
		 if(id>0){
			saveLog(LevelEnum.system, "修改治疗方式(id:"+bean.getId()+"-"+bean.getName()+")");
			run_success("修改成功！");
		}else {run_false("修改失败！");}
		
	}
}
